package cn.touna.jss.common.config;

import cn.touna.jss.common.properties.config.DBProperties;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * 数据库统计监控
 * Created by wuwc on 2017/10/19.
 */
//@Configuration
public class DruidConfiguration {

    @Autowired
    private DBProperties dbProperties;

    /**
     * 注册一个StatViewServlet
     *
     * @return
     */
    /*@Bean
    public ServletRegistrationBean DruidStatViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid*//*");
        //白名单：
        //servletRegistrationBean.addInitParameter("allow", dbProperties.getAllowIp());
        //IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        //servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
        servletRegistrationBean.addInitParameter("loginUsername", dbProperties.getLoginName());
        servletRegistrationBean.addInitParameter("loginPassword", dbProperties.getLoginPassword());
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", dbProperties.getResetEnable());
        return servletRegistrationBean;
    }*/

    /**
     * 注册一个：filterRegistrationBean
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean druidStatFilter() {

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}
